<template>
	<view class="loginToast">
		<view class="title">提示</view>
		<view class="tips">
		 	<text>登录后才能体验更多功能</text>
			<view>是否现在登录？</view>
		</view>
		<view class="btns dFlex jBetween_aCenter">
			<view class="btn" @click.stop='closeLoginTaost'>取消</view>
			<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"  class="btn login">现在登录</button>
		</view>
	</view>
</template>

<script>
	import {
		jscode2session,programLogin
	} from '@/tools/http.js';
	export default {
		name:"loginToast",
		data() {
			return {
				
			};
		},
		methods:{
			// 关闭登录
			closeLoginTaost(){
				this.$emit('closeLogin','');
			},
			// 登录
			getPhoneNumber(e){
				console.log(e);
				if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户决绝授权
					uni.showToast({
						title:'用户拒绝授权',
						icon:"none"
					})
				} else {
					wx.login({
						success:async (login)=>{
							let res = await jscode2session({
								code:login.code,
							});
							if(res){
								let res2 = await programLogin({
									encrypt: e.detail.encryptedData,
									iv: e.detail.iv,
									session_key: JSON.parse(res).session_key,
									openid: JSON.parse(res).openid,
									tjr_mobile:uni.getStorageSync('tjr_mobile')?uni.getStorageSync('tjr_mobile'):'',
									unionid:JSON.parse(res).unionid,
									source:'0',
								});
								if(res2){
									uni.setStorageSync('uToken',res2.userinfo.token);
									this.$emit('closeLogin','');
								}
							}
						},
						fail:(err)=>{
							console.log(err);
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.loginToast{
		width: 70vw;
		border-radius: 4px;
		background:white;
		overflow: hidden;
		.btns{
			width: 100%;
			.btn{
				flex:1;
				text-align: center;
				padding:8px 0;
				color:#c8c8c8;
				font-size: 13px;
			}
			.login{
				background: #2979ff;
				color:white;
			}
		}
		.tips{
			width: 100%;
			padding: 20px;
			text-align: center;
			box-sizing: border-box;
			line-height: 20px;
		}
		.title{
			width: 100%;
			text-align: center;
			padding:10px 0;
			border-bottom: 1px solid rgba(0,0,0,0.1);
		}
	}
</style>